package com.sentiment.algorithm;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * Created by vani:liaozhenlin on 15-4-15. If you meet any question,you can
 * email at 939899727@qq.com
 *
 * @author 寤栬礊鏋� from uestc
 */
public class KmpMethod {
	private String pattern_text;
	private String pattern;
	private int pattern_para;
	private int pattern_sentences;
	private String article_text;
	private int article_para;
	private int article_sentences;
	private int point = 0;
	private ArrayList<Recoed> recoeds = new ArrayList<Recoed>();

	public KmpMethod() {

	}

	public ArrayList<Recoed> getRecoeds() {
		return recoeds;
	}

	public void setRecoeds(ArrayList<Recoed> recoeds) {
		this.recoeds = recoeds;
	}

	public KmpMethod(String pattern_text, int pattern_para, String article_text, int article_para) {
		this.pattern_text = pattern_text;
		this.pattern_para = pattern_para;
		this.article_text = article_text;
		this.article_para = article_para;

	}

	public String getSentences(String str) {
		str = str.replaceAll("-", "");
		return str.replaceAll("[？！。；]", "-");
	}

	public static void main(String[] args) {

		String str = "";
		String str1 = "";
		KmpMethod kmpMethod = new KmpMethod(str, 9, str1, 0);
		kmpMethod.selfPattern();
		System.out.println(kmpMethod.recoeds);
	}

	public int getKmpPoint(String str) {
		Kmp kmp = new Kmp(str, pattern);
		int tag = kmp.kmpSelf();
		return tag;
	}

	public void selfPattern() {
		String[] ps = getSentences(pattern_text).split("-");
		String[] as = getSentences(article_text).split("-");
		pattern_sentences = ps.length;
		article_sentences = as.length;

		for (int pp = 0; pp < pattern_sentences; pp++) {
			int len = ps[pp].length();
			if (len < 7) {
				continue;
			}

			for (int ap = 0; ap < article_sentences; ap++) {
				int tag = -1;
				if (as[ap].length() < 7) {
					continue;
				}
				for (int i = 0; i < len - 6; i++) {
					point = i;
					StringBuilder stringBuilder = new StringBuilder();
					for (int j = 0; j < 7; j++) {
						stringBuilder.append(ps[pp].charAt(i + j));
					}
					pattern = stringBuilder.toString();
					tag = getKmpPoint(as[ap]);
					if (tag == -1) {
						continue;
					} else {
						double simi = similarLike(ps[pp], as[ap], tag, point);
						Recoed recoed = new Recoed(pp, ap, pattern_para, article_para);
						recoed.setSimilar(simi);
						recoeds.add(recoed);
						break;
					}
				}

			}

		}

	}

	public double similarLike(String pp, String ap, int tag, int pos) {
		int count = 7;
		int length = ap.length();
		tag = tag + 7;
		pos = pos + 7;
		while ((pos) < pp.length() && (tag) < length) {
			if (pp.charAt(pos) == ap.charAt(tag)) {
				tag++;
				count++;
				pos++;
			} else {
				break;
			}
		}

		/*
		 * 杩欐浠ｇ爜鏍规嵁鍏蜂綋鎯呭喌鑰屽畾
		 */
		TreeMap<Character, Boolean> treeMap = new TreeMap<Character, Boolean>();
		for (int i = tag; i < ap.length(); i++) {
			treeMap.put(ap.charAt(i), true);
		}

		for (int j = pos; j < pp.length(); j++) {
			if (treeMap.containsKey(pp.charAt(j))) {
				count++;
			}
		}

		return (double) (count) / (double) (length);
	}

	class Recoed {
		int point;
		int tag;
		int pattern_para;
		int text_para;
		double similar;

		public Recoed(int point, int tag, int pattern_para, int text_para) {
			this.point = point;
			this.tag = tag;
			this.pattern_para = pattern_para;
			this.text_para = text_para;
		}

		public double getSimilar() {
			return similar;
		}

		public void setSimilar(double similar) {
			this.similar = similar;
		}

		public int getPoint() {
			return point;
		}

		public void setPoint(int point) {
			this.point = point;
		}

		public int getTag() {
			return tag;
		}

		public void setTag(int tag) {
			this.tag = tag;
		}

		public int getPattern_para() {
			return pattern_para;
		}

		public void setPattern_para(int pattern_para) {
			this.pattern_para = pattern_para;
		}

		public int getText_para() {
			return text_para;
		}

		public void setText_para(int text_para) {
			this.text_para = text_para;
		}

		@Override
		public String toString() {
			return "Recoed{" + "pattern=" + point + ", article=" + tag + ", pattern_para=" + pattern_para
					+ ", text_para=" + text_para + ", similar=" + similar + '}' + "\n";
		}
	}
}
